package com.smile.service.entity.organization;

import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;

import java.util.List;

@Data
public class AirportBindDevicesData {

    @JSONField(name = "bind_devices")
    private List<BindDevice> bindDevices;

    // 嵌套类 - 绑定设备信息
    @Data
    public static class BindDevice {

        /**
         * 设备绑定码
         */
        @JSONField(name = "device_binding_code")
        private String bindingCode;

        /**
         * 设备在组织中的名称
         */
        @JSONField(name = "device_callsign")
        private String callsign;

        /**
         * 0-100-1
         */
        @JSONField(name = "device_model_key")
        private String modelKey;

        /**
         * 组织 ID
         */
        @JSONField(name = "organization_id")
        private String organizationId;

        /**
         * 设备序列号（SN）
         */
        @JSONField(name = "sn")
        private String serialNumber;

        // Getters & Setters
        public String getBindingCode() {
            return bindingCode;
        }

        public void setBindingCode(String bindingCode) {
            this.bindingCode = bindingCode;
        }

        public String getCallsign() {
            return callsign;
        }

        public void setCallsign(String callsign) {
            this.callsign = callsign;
        }

        public String getModelKey() {
            return modelKey;
        }

        public void setModelKey(String modelKey) {
            this.modelKey = modelKey;
        }

        public String getOrganizationId() {
            return organizationId;
        }

        public void setOrganizationId(String organizationId) {
            this.organizationId = organizationId;
        }

        public String getSerialNumber() {
            return serialNumber;
        }

        public void setSerialNumber(String serialNumber) {
            this.serialNumber = serialNumber;
        }

        @Override
        public String toString() {
            return "BindDevice{" +
                    "bindingCode='" + bindingCode + '\'' +
                    ", callsign='" + callsign + '\'' +
                    ", modelKey='" + modelKey + '\'' +
                    ", organizationId='" + organizationId + '\'' +
                    ", serialNumber='" + serialNumber + '\'' +
                    '}';
        }

        // 实用方法 - 判断设备类型（根据modelKey）
        public String getDeviceType() {
            if (modelKey != null) {
                if (modelKey.startsWith("3-")) return "Dock";
                if (modelKey.startsWith("0-")) return "Drone";
            }
            return "Unknown";
        }
    }

    // Getters & Setters
    public List<BindDevice> getBindDevices() {
        return bindDevices;
    }

    public void setBindDevices(List<BindDevice> bindDevices) {
        this.bindDevices = bindDevices;
    }

    // 实用方法 - 根据设备类型获取设备列表
    public List<BindDevice> getDevicesByType(String typeKeyword) {
        if (bindDevices == null) return List.of();

        return bindDevices.stream()
                .filter(device -> device.getDeviceType().equalsIgnoreCase(typeKeyword))
                .toList();
    }

    // 实用方法 - 获取所有设备序列号
    public List<String> getAllDeviceSerials() {
        if (bindDevices == null) return List.of();

        return bindDevices.stream()
                .map(BindDevice::getSerialNumber)
                .toList();
    }

    // 实用方法 - 获取所有Dock设备
    public List<BindDevice> getDockDevices() {
        return getDevicesByType("Dock");
    }

    // 实用方法 - 获取所有Drone设备
    public List<BindDevice> getDroneDevices() {
        return getDevicesByType("Drone");
    }

    // 实用方法 - 根据序列号查找设备
    public BindDevice findDeviceBySn(String sn) {
        if (bindDevices == null) return null;

        return bindDevices.stream()
                .filter(device -> sn.equalsIgnoreCase(device.getSerialNumber()))
                .findFirst()
                .orElse(null);
    }

    @Override
    public String toString() {
        return "AirportBindDevicesData{" +
                "bindDevices=" + bindDevices +
                '}';
    }
}